package com.tkjt.pms.persist.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.calc.common.IndexCalcConsts;
import com.tkjt.pms.calc.common.IndexCalcParallel;
import com.tkjt.pms.calc.formula.common.FormulaRule;
import com.tkjt.pms.calc.formula.common.EngineResult;
import com.tkjt.pms.calc.formula.common.FormulaValue;
import com.tkjt.pms.calc.formula.utils.FormulaParamHelper;
import com.tkjt.pms.common.IndexParam;
import com.tkjt.pms.common.utils.EsJsonUtils;
import com.tkjt.pms.persist.dao.IndexCalcTraceDao;
import com.tkjt.pms.persist.entity.TkIndexCalcTrace;
import com.tkjt.pms.persist.mapper.TkIndexCalcTraceMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Transactional
@Repository("indexCalcTraceDao")
public class IndexCalcTraceDaoImpl implements IndexCalcTraceDao {

	@Autowired
	private TkIndexCalcTraceMapper tkIndexCalcTraceMapper;

	@Override
	public void updateCalcTraceSuccess(IndexParam indexParam, IndexCalcParallel indexCalcParallel) {

		//log.debug(EsJsonUtils.toJsonStr(indexParam));
		log.debug("indexParam={}",indexParam);
		
		this.updateCalcTrace(IndexCalcConsts.RET_CALC_STATUS_SUCCESS, indexParam, indexCalcParallel, null);

	}
	
	@Override
	public void updateCalcTraceException(IndexParam indexParam, IndexCalcParallel indexCalcParallel, String exceptionMsg) {
		
		this.updateCalcTrace(IndexCalcConsts.RET_CALC_STATUS_FAIL, indexParam, indexCalcParallel, exceptionMsg);
		
	}

	@Override
	public void updateCalcTraceException(IndexParam indexParam, IndexCalcParallel indexCalcParallel) {
		
		String currentWhichData = FormulaParamHelper.getFormulaCurrentWhichData(indexParam);
		String exceptionMsg = FormulaParamHelper.getFormulaException(indexParam, currentWhichData);

		this.updateCalcTrace(IndexCalcConsts.RET_CALC_STATUS_FAIL, indexParam, indexCalcParallel, exceptionMsg);

	}

	public void updateCalcTrace(Integer calcStatus, IndexParam indexParam, IndexCalcParallel indexCalcParallel, String exceptionMsg) {

		String cbatchCode = (String) indexParam.getParam(IndexCalcConsts.PARAM_CALC_CBATCH_CODE);
		String pgroupCode = (String) indexParam.getParam(IndexCalcConsts.PARAM_CALC_CBATCH_CODE);

		/*boolean hasTrace = true;
		QueryWrapper<TkIndexCalcTrace> queryWrapper = this.buildQueryWrapper(cbatchCode, pgroupCode, indexCalcParallel);
		TkIndexCalcTrace tkIndexCalcTrace = tkIndexCalcTraceMapper.selectOne(queryWrapper);
		if (null == tkIndexCalcTrace) {
			hasTrace = false;
			tkIndexCalcTrace = new TkIndexCalcTrace();
		}
		tkIndexCalcTrace.setCalcStatus(calcStatus);
		tkIndexCalcTrace.setCalcLogs(exceptionMsg);

		
		tkIndexCalcTrace = this.buildTkIndexCalcTrace(tkIndexCalcTrace, indexCalcParallel, indexParam);

		if (hasTrace) {
			tkIndexCalcTraceMapper.updateById(tkIndexCalcTrace);
		} else {
			tkIndexCalcTraceMapper.insert(tkIndexCalcTrace);
		}*/

		TkIndexCalcTrace tkIndexCalcTrace = new TkIndexCalcTrace();
		tkIndexCalcTrace.setCalcStatus(calcStatus);
		tkIndexCalcTrace.setCalcLogs(exceptionMsg);
		tkIndexCalcTrace = this.buildTkIndexCalcTrace(tkIndexCalcTrace, indexCalcParallel, indexParam);
		tkIndexCalcTraceMapper.insert(tkIndexCalcTrace);
	}

	private TkIndexCalcTrace buildTkIndexCalcTrace(TkIndexCalcTrace tkIndexCalcTrace, IndexCalcParallel indexCalcParallel, IndexParam indexParam) {

		String cbatchCode = (String) indexParam.getParam(IndexCalcConsts.PARAM_CALC_CBATCH_CODE);
		String pgroupCode = (String) indexParam.getParam(IndexCalcConsts.PARAM_CALC_PGROUP_CODE);
		tkIndexCalcTrace.setCbatchCode(cbatchCode);
		tkIndexCalcTrace.setPgroupCode(pgroupCode);
		tkIndexCalcTrace.setIndexValueCode(indexCalcParallel.getIndexValueCode());
		tkIndexCalcTrace.setIndexYearMonth(indexCalcParallel.getIndexYearMonth());
		tkIndexCalcTrace.setIndexDepart(indexCalcParallel.getIndexDepart());
		tkIndexCalcTrace.setIndexScorer(indexCalcParallel.getIndexScorer());
		
		Date calcTimeStart = (Date) indexParam.getParam(IndexCalcConsts.PARAM_CALC_TIME_START);
		Date calcTimeDepen = (Date) indexParam.getParam(IndexCalcConsts.PARAM_CALC_TIME_DEPEN);
		Date calcTimeEnd = (Date) indexParam.getParam(IndexCalcConsts.PARAM_CALC_TIME_END);
		tkIndexCalcTrace.setCalcTimeStart(calcTimeStart);
		tkIndexCalcTrace.setCalcTimeDepen(calcTimeDepen);
		tkIndexCalcTrace.setCalcTimeEnd(calcTimeEnd);
		
		List<String> configWhichDatas = FormulaParamHelper.getCalcWhichDatas(indexParam);
		//公式信息
		Map<String, Object> tempData = new HashMap<String, Object>();
		for(String whichData : configWhichDatas) {
			FormulaRule formulaRule = FormulaParamHelper.getFormulaRule(indexParam, whichData);
			if(null != formulaRule) {
				tempData.put(whichData, formulaRule);
			}
		}
		tkIndexCalcTrace.setCalcFormula(EsJsonUtils.toJsonStr(tempData));
		//公式计算结果
		tempData = new HashMap<String, Object>();
		for(String whichData : configWhichDatas) {
			EngineResult engineResult = FormulaParamHelper.getFormulaResult(indexParam, whichData);
			if(null != engineResult) {
				tempData.put(whichData, engineResult);
			}
		}
		tkIndexCalcTrace.setCalcResult(EsJsonUtils.toJsonStr(tempData));
		//公式计算数据
		tempData = new HashMap<String, Object>();
		for(String whichData : configWhichDatas) {
			FormulaValue formulaValue = FormulaParamHelper.getFormulaValue(indexParam, whichData);
			if(null != formulaValue) {
				tempData.put(whichData, formulaValue);
			}
		}
		tkIndexCalcTrace.setCalcParam(EsJsonUtils.toJsonStr(tempData));
		//异常信息
		tempData = new HashMap<String, Object>();
		for(String whichData : configWhichDatas) {
			String exception = FormulaParamHelper.getFormulaException(indexParam, whichData);
			if(null != exception) {
				tempData.put(whichData, exception);
			}
		}
		if(!tempData.isEmpty()) {
			tkIndexCalcTrace.setCalcLogs(EsJsonUtils.toJsonStr(tempData));
		}

		return tkIndexCalcTrace;
	}

	private QueryWrapper<TkIndexCalcTrace> buildQueryWrapper(String cbatchCode, String pgroupCode, IndexCalcParallel indexCalcParallel) {
		QueryWrapper<TkIndexCalcTrace> queryWrapper = new QueryWrapper<>();

		queryWrapper.lambda().eq(TkIndexCalcTrace::getIndexYearMonth, indexCalcParallel.getIndexYearMonth());
		queryWrapper.lambda().eq(TkIndexCalcTrace::getIndexValueCode, indexCalcParallel.getIndexValueCode());
		queryWrapper.lambda().eq(TkIndexCalcTrace::getIndexDepart, indexCalcParallel.getIndexDepart());
		queryWrapper.lambda().eq(TkIndexCalcTrace::getIndexScorer, indexCalcParallel.getIndexScorer());

		queryWrapper.lambda().eq(TkIndexCalcTrace::getCbatchCode, cbatchCode);
		queryWrapper.lambda().eq(TkIndexCalcTrace::getPgroupCode, pgroupCode);

//		queryWrapper.eq("indexYearMonth", indexYearMonth);
//		queryWrapper.eq("indexValueCode", indexValueCode);
//		queryWrapper.eq("indexDepart", indexDepart);
//		queryWrapper.eq("indexScorer", indexScorer);
//
//		queryWrapper.eq("cbatchCode", cbatchCode);
//		queryWrapper.eq("pgroupCode", pgroupCode);

		return queryWrapper;
	}

}
